package com.xcity.game.combat.calc;

import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.unit.Combatable;

/**
 * 属性计算器<br/>
 * 只在初始化或属性发生变化时计算
 * @author yang.li
 *
 */
public abstract class CombatAttributeCalculator<C extends Combatable> {

	protected C unit;
	
	protected float attackPower;
	protected float magicPower;
	protected float defense;
	protected float magicDefense;
	protected float crit;
	protected float avoidCrit;
	protected float hp;
	protected float attackSpeed;
	protected float dodge;
	protected float resist;
	protected float hitRate;
	protected float magicHitRate;
	
	protected boolean calculated;
	
	public CombatAttributeCalculator(C unit) {
		this.unit = unit;
		this.calculated = false;
	}
	
	public float getValue(CombatAttributeType type) {
		type = doFilterType(type);
		if (!calculated) {
			calc();
		}
		if (type == CombatAttributeType.ATTACK_POWER) {
			return attackPower;
		}
		if (type == CombatAttributeType.MAGIC_POWER) {
			return magicPower;
		}
		if (type == CombatAttributeType.DEFENSE) {
			return defense;
		}
		if (type == CombatAttributeType.MAGIC_DEFENSE) {
			return magicDefense;
		}
		if (type == CombatAttributeType.CRIT) {
			return crit;
		}
		if (type == CombatAttributeType.AVOID_CRIT) {
			return avoidCrit;
		}
		if (type == CombatAttributeType.HP) {
			return hp;
		}
		if (type == CombatAttributeType.ATTACK_SPEED) {
			return attackSpeed;
		}
		if (type == CombatAttributeType.DODGE) {
			return dodge;
		}
		if (type == CombatAttributeType.RESIST) {
			return resist;
		}
		if (type == CombatAttributeType.HIT_RATE) {
			return hitRate;
		}
		if (type == CombatAttributeType.MAGIC_HIT_RATE) {
			return magicHitRate;
		}
		return 0f;
	}
	
	private static CombatAttributeType doFilterType(CombatAttributeType type) {
		if (type == CombatAttributeType.MAX_HP) {
			return CombatAttributeType.HP;
		}
		if (type == CombatAttributeType.MAX_MP) {
			return CombatAttributeType.MP;
		}
		return type;
	}

	/** 初始化或属性发生变化时调用计算 */
	public void calc() {
		attackPower = calc(CombatAttributeType.ATTACK_POWER);
		magicPower = calc(CombatAttributeType.MAGIC_POWER);
		defense = calc(CombatAttributeType.DEFENSE);
		magicDefense = calc(CombatAttributeType.MAGIC_DEFENSE);
		crit = calc(CombatAttributeType.CRIT);
		avoidCrit = calc(CombatAttributeType.AVOID_CRIT);
		hp = calc(CombatAttributeType.HP);
		attackSpeed = calc(CombatAttributeType.ATTACK_SPEED);
		dodge = calc(CombatAttributeType.DODGE);
		resist = calc(CombatAttributeType.RESIST);
		hitRate = calc(CombatAttributeType.HIT_RATE);
		magicHitRate = calc(CombatAttributeType.MAGIC_HIT_RATE);
		calculated = true;
	}
	
	protected abstract float calc(CombatAttributeType type);
	
}
